<?php

namespace Admin\Controller;

//use Think\Controller;

class IndexController extends ExtendController {

    //显示栏目
    public function index(){
        
       /* import('ORG.Util.Page'); // 导入分页类
        $count = $Subject->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数

        $show = $Page->show();     // 分页显示输出*/
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
     /*   $imagesList = $imagesModel->order('id DESC')->select();
        $this->assign('list', $imagesList); // 赋值数据集*/
     /*   $this->assign('page', $show); // 赋值分页输出*/
     $imagesModel = D('Img');
     $list = $imagesModel->order('createtime desc')->select();
     $namelist = D('imgName')->field('name_name,name_id')->select();
     $name =array();
     foreach ($list as $key => $value) {
        //获取图片中的姓名
        $name_id = explode(',', $value['name_id']);
         foreach ($name_id as $key2 => $value2) {
            foreach ($namelist as $key3 => $value3) {
                //echo $value2."+++".$value3['name_id']."</br>";
                if($value2 == $value3['name_id']){
                    //$name[$key2]= $value3['name_name'];
                    $namestr[$key] .= $value3['name_name'].',';
                    //echo $value2."+++".$value3['name_id']."</br>";
                }

            }
         }
         $namestr[$key] =mb_substr($namestr[$key], 0,-1);
         $value['namestr'] =$namestr[$key];
     

         //获取图片中的类型
           if(!empty($value['type_id'])){
           $typename = D('ImgType')->field('type_name')->where("type_id=".$value['type_id'])->find();
           $value['typename'] = $typename['type_name'];
           }



         //获取图片的组图
           if(!empty($value['zutu_id'])){
              $zutuame = D('Zutu')->field('zutu_name')->where('zutu_id='.$value['zutu_id'])->find();
             $value['zutuname'] = $zutuame['zutu_name'];
           }


         //获取图片的组图
           if(!empty($value['ctype_id'])){
              $ctypename = D('Ctype')->field('ctype_name')->where('ctype_id='.$value['ctype_id'])->find();
             $value['ctypename'] = $ctypename['ctype_name'];
           }
       $list[$key] = $value;
     }
     $this->assign('list',$list);
        $this->display(); // 输出模板
    }
   

   public function  insert(){
   $imgNameList = D('ImgName')->select();
   if(!$imgNameList){
     $this->error('没有图片名');
   }
   $imgTypeList = D('ImgType')->select();
   if(!$imgTypeList){
     $this->error('没有图片类型');
   }
   $imgzutuList = D('Zutu')->Select();
   if(!$imgzutuList){
     $this->error('没有图片分组');
   }

     $imgCtypeList = D('Ctype')->select();
   if(!$imgCtypeList){
     $this->error('没有图片内容类型');
   }
   $this->assign('ctype',$imgCtypeList);
   $this->assign('name',$imgNameList);
   $this->assign('type',$imgTypeList);
   $this->assign('zutu',$imgzutuList);
    $this->display('insert');
   }
    //添加
    public function add() {
       
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize = 3145728;
      $upload->rootPath = './Public/img/';
      $upload->savePath = '';
      $upload->saveName = array('uniqid','');
      $upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
      $upload->autoSub  = true;
      $upload->subName  = array('date','Ymd');
       // 上传文件 
      $info   =   $upload->upload($_FILES);
      if(!$info) {// 上传错误提示错误信息
         $this->error($upload->getError());
      }else{// 上传成功 获取上传文件信息
         //保存缩略图
        $images = new \Think\Image();

           $images->open('./Public/img/'.$info['image_url']['savepath'].$info['image_url']['savename']);
           $newsavename = str_replace('.','_thumb.',$info['image_url']['savename']);
           $wid = 180;
           $hei = 180;
           $vvv = $images->thumb($wid, $hei,\Think\Image::IMAGE_THUMB_FIXED)->save('./Public/img/'.$info['image_url']['savepath'].$newsavename);
           $vv = $images->thumb($wid, $hei,\Think\Image::IMAGE_THUMB_FIXED)->save('./Public/img/'.$info['image_url']['savepath'].$newsavename);
           $info['image_url']['savepath_thumb'] = "./Public/img/".$info['image_url']['savepath'];//缩略图显示路径
           $info['image_url']['savethumbname_thumb'] = $newsavename;
         foreach($info as $file){
             $imgurl= $file['savepath'].$file['savename'];
             $data['url'] = "img/".$imgurl;
             $data['thumb_url'] = "img/".$file['savepath'].$file['savethumbname_thumb'];
           }
        foreach ($_POST as $key => $value) {
            if($key =="name_".$value){
                $name .= $value.',';
            }
        }
        $name = mb_substr($name,0,-1);

        $data['name_id'] = $name;
        $data['type_id'] = $_POST['type'];
        $data['ctype_id'] = $_POST['ctype'];
        $data['title'] = $_POST['title'];
        //$data['url'] = $_POST['image_url'];
        $data['zutu_id'] = $_POST['zutu'];
        $data['sort'] = $_POST['sort'];
        $data['img_time'] = $_POST['time'];
        $data['content'] = $_POST['message'];
        $data['createtime'] =date('Y-m-d H:i:s');
        $result = D('Img')->add($data);

        if ($result != false) {
            $this->redirect('index');
        } else {
            $this->error('非法操作!');
        }
      }
    }

  
      //修改
     public function update() {
        if(IS_POST){
            $where['id'] = $_POST['id'];
                    foreach ($_POST as $key => $value) {
                        if($key =="name_".$value){
                            $name .= $value.',';
                        }
                    }
                    $name = mb_substr($name,0,-1);
                    $data['name_id'] = $name;
                    $data['type_id'] = $_POST['type'];
                    $data['ctype_id'] = $_POST['ctype'];
                    $data['title'] = $_POST['title'];
                    $data['url'] = $_POST['image_url'];
                    $data['zutu_id'] = $_POST['zutu'];
                    $data['sort'] = $_POST['sort'];
                    $data['img_time'] = $_POST['time'];
                    $data['content'] = $_POST['message'];
                    $data['createtime'] =date('Y-m-d H:i:s');
                    $result = D('Img')->where($where)->save($data);
                   
                    if ($result != false) {
                        $this->redirect('index');
                    } else {
                        $this->error('非法操作!');
                    }
        }else{
            //获取到该条记录的内容
             $list= D('Img')->where('id = '.$_GET['id'])->find();
             if(!$list){
                $this->error('非法操作!');
             }
             $this->assign('list',$list);
               $imgNameList = D('ImgName')->select();
               if(!$imgNameList){
                 $this->error('没有图片名');
               }
               $imgTypeList = D('ImgType')->select();
               if(!$imgTypeList){
                 $this->error('没有图片类型');
               }
               $imgzutuList = D('Zutu')->Select();
               if(!$imgzutuList){
                 $this->error('没有图片分组');
               }
                    $imgCtypeList = D('Ctype')->select();
               if(!$imgCtypeList){
                 $this->error('没有图片内容类型');
               }
               $this->assign('ctype',$imgCtypeList);
               $this->assign('name',$imgNameList);
               $this->assign('type',$imgTypeList);
               $this->assign('zutu',$imgzutuList);
                $this->display('updata');
                }
}
    
    //删除
    public function delete() {
        $imagesModel = M('Img');
        $map['id'] = $_GET['id'];
        $result = $imagesModel->where($map)->delete();
        if ($result != false) {
            $this->redirect('Index/index');
        } else {
            $this->error('删除失败!');
        }
    }

}